<?php

namespace App\Services\WechatApi;

use App\Services\ServiceBase;
use Illuminate\Support\Facades\Http;
use YQ\YqCurl;

class WechatApiBase extends ServiceBase
{
    protected $host = 'https://api.weixin.qq.com/';

    public function send($api, $params, $method = 'get')
    {
        $url = "{$this->host}{$api}";
        if ($method == 'get') {
            $url = "{$url}?" . http_build_query($params);
            $response = Http::get($url);
        } else {
            $response = Http::post($url, $params);
        }

        if (!$response->ok()) {
            return false;
        }

        $res = json_decode($response->body(), true);
        if (isset($res['errcode']) && $res['errcode'] != 0) {
            return false;
        }

        return $res;
    }

    public function send2($api, $params, $method = 'get')
    {
        $url = "{$this->host}{$api}";
        if ($method == 'get') {
            $response = YqCurl::curl($url, $params, 1, 0, 5);
        } else {
            $response = YqCurl::curl($url, $params, 1, 1, 5);
        }

        if (!$response) {
            return false;
        }

        $res = json_decode($response, true);
        if (isset($res['errcode']) && $res['errcode'] != 0) {
            return false;
        }

        return $res;
    }

    // 登录凭证校验
    // https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.code2Session.html
    public function code2Session($appid, $secret, $js_code)
    {
        $api = "sns/jscode2session";
        return $this->send($api, [
            'appid' => $appid,
            'secret' => $secret,
            'js_code' => $js_code,
            'grant_type' => 'authorization_code',
        ]);
    }

    // 获取小程序全局唯一后台接口调用凭据
    // https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/access-token/auth.getAccessToken.html
    public function getAccessToken($appid, $secret)
    {
        $api = "cgi-bin/token";
        return $this->send($api, [
            'grant_type' => 'client_credential',
            'appid' => $appid,
            'secret' => $secret,
        ]);
    }

    // 获取小程序全局唯一后台接口调用凭据
    // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
    public function getStableAccessToken($appid, $secret)
    {
        $api = "cgi-bin/stable_token";
        return $this->send($api, [
            'grant_type' => 'client_credential',
            'appid' => $appid,
            'secret' => $secret,
        ], 'post');
    }

    // 获取小程序 URL Link
    // https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/url-link/urllink.generate.html
    public function generateUrllink($access_token, $path = '', $query = '')
    {
        $api = "wxa/generate_urllink?access_token={$access_token}";
        return $this->send($api, [
            'path'            => $path,
            'query'           => $query,
            'expire_type'     => 1,
            'expire_interval' => 30,
        ], 'post');
    }

    // 获取小程序 scheme 码
    // https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/url-scheme/urlscheme.generate.html
    public function generateUrlScheme($access_token, $path = '', $query = '')
    {
        $api = "wxa/generatescheme?access_token={$access_token}";
        return $this->send($api, [
            'jump_wxa' => [
                'path'  => $path,
                'query' => $query,
            ],
            'expire_type' => 1,
            'expire_interval' => 30,
        ], 'post');
    }
}
